//============================================================================================
/**
 * @file	phclink_init.c
 * @brief	ＰＨＣ−ＤＳ接続画面 PHC初期化 【シーケンス】
 * @author	Akito Mori
 * @date	09.02.02
 */
//============================================================================================
#include "common.h"
#include "gflib/touchpanel.h"
#include "system/gamedata.h"
#include "system/procsys.h"
#include "system/main.h"
#include "system/fontproc.h"
#include "system/window.h"
#include "system/pmfprint.h"
#include "system/snd_tool.h"
#include "system/arc_tool.h"
#include "system/arc_util.h"
#include "system/clact_tool.h"
#include "system/clact_util.h"
#include "system/msgdata.h"
#include "system/wordset.h"
#include "system/mystatus.h"
#include "system/wipe.h"
#include "savedata/config.h"
#include "itemtool/item.h"
#include "itemtool/myitem.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_phc_com.h"

#include "application/phclink.h"
#include "phclink_obj_def.h"
#include "phclink_bmp_def.h"
#include "phclink_local.h"
#include "phclink_obj.h"
#include "phclink_bmp.h"
#include "phc_ds.naix"

enum{
	SUBSEQ_MSG=0,
	SUBSEQ_YESNO_INIT,
	SUBSEQ_YESNO_WAIT,
	SUBSEQ_SELECT_INIT,
	SUBSEQ_SELECT_MSG_WAIT,
	SUBSEQ_SELECT_WAIT,
	SUBSEQ_SELECT_ACTION,

	SUBSEQ_WALK_CLEAR_MSG,
	SUBSEQ_WALK_CLEAR_MSG_WAIT,
	SUBSEQ_WALK_CLEAR_YESNO_INIT,
	SUBSEQ_WALK_CLEAR_YESNO_WAIT,

	SUBSEQ_START_INITIALIZE_MSG,
	SUBSEQ_START_INITIALIZE_MSG_WAIT,

	SUBSEQ_INITIALIZE_MSG,
	SUBSEQ_INITIALIZE_MSG_WAIT,
	SUBSEQ_IRC_BOOT,
	SUBSEQ_IRC_COMMAND,
	SUBSEQ_IRC_WAIT,
	SUBSEQ_IRC_ERROR_MSG,
	SUBSEQ_IRC_ERROR_MSG_WAIT,
	SUBSEQ_IRC_SUCCESS_MSG,
	SUBSEQ_IRC_SUCCESS_MSG_WAIT,

	SUBSEQ_OBLIGATE_RECV_MSG,
	SUBSEQ_OBLIGATE_RECV_MSG_WAIT,
	SUBSEQ_OBLIGATE_RECV_DEMO_INIT,
	SUBSEQ_OBLIGATE_RECV_DEMO,
	SUBSEQ_OBLIGATE_RECV_DEMO_END,
	SUBSEQ_OBLIGATE_RECV_DEMO_END_MSG,
	SUBSEQ_OBLIGATE_RECV_DEMO_END_MSG_WAIT,

	SUBSEQ_INIT_SAVE_MSG,
	SUBSEQ_INIT_SAVE_MSG_WAIT,
	SUBSEQ_INIT_SAVE_START,
	SUBSEQ_INIT_SAVE_END,

	SUBSEQ_CURSOR_ANIME_WAIT,
	SUBSEQ_END,
	SUBSEQ_MAX,
};


static int SubSeqMsg( PHCLINK_WORK *wk );
static int SubSeqYesNoInit( PHCLINK_WORK *wk );
static int SubSeqYesNoWait( PHCLINK_WORK *wk );
static int SubSeqSelectInit( PHCLINK_WORK *wk );
static int SubSeqSelectMsgWait( PHCLINK_WORK *wk );
static int SubSeqSelectWait( PHCLINK_WORK *wk );
static int SubSeqSelectAction( PHCLINK_WORK *wk );
static int SubSeqStartInitializeMsg( PHCLINK_WORK *wk );
static int SubSeqStartInitializeMsgWait( PHCLINK_WORK *wk );
static int SubSeqInitializeMsg( PHCLINK_WORK *wk );
static int SubSeqInitializeMsgWait( PHCLINK_WORK *wk );
static int SubSeq_Irc_Boot( PHCLINK_WORK *wk );
static int SubSeq_Irc_Command( PHCLINK_WORK *wk );
static int SubSeq_Irc_Wait( PHCLINK_WORK *wk );
static int SubSeq_IrcErrorMsg( PHCLINK_WORK *wk );
static int SUBSEQ_IrcErrorMsgWait( PHCLINK_WORK *wk );
static int SubSeq_IrcSuccessMsg( PHCLINK_WORK *wk );
static int SUBSEQ_IrcSuccessMsgWait( PHCLINK_WORK *wk );
static int SubSeqEnd( PHCLINK_WORK *wk );
static int SubSeq_init_save_msg( PHCLINK_WORK *wk );
static int SubSeq_init_save_msg_wait( PHCLINK_WORK *wk );
static int SubSeq_init_save_start( PHCLINK_WORK *wk );
static int SubSeq_init_save_end( PHCLINK_WORK *wk );

static int 	SubSeq_ObligateRecvMsg( PHCLINK_WORK *wk );
static int 	SubSeq_ObligateRecvMsgWait( PHCLINK_WORK *wk );
static int SubSeq_OBligateRecvEndMsgWait( PHCLINK_WORK *wk );
static int SubSeq_ObligateRecvDemoInit( PHCLINK_WORK *wk );
static int SubSeq_OBligateRecvDemo( PHCLINK_WORK *wk );
static int SubSeq_OBligateRecvDemoEnd( PHCLINK_WORK *wk );
static int SubSeq_ObligateRecvDemoEndMsg( PHCLINK_WORK *wk );
static int SubSeq_ObligateRecvDemoEndMsgWait( PHCLINK_WORK *wk );


static int	SubSeqWalkClearMsg( PHCLINK_WORK *wk );
static int	SubSeqWalkClearMsgWait( PHCLINK_WORK *wk );
static int	SubSeqWalkClearYesnoInit( PHCLINK_WORK *wk );
static int	SubSeqWalkClearYesnoWait( PHCLINK_WORK *wk );

static int SubSeqCursorAnimeWait( PHCLINK_WORK *wk );

static void _deposit_pokemon_check( PHCLINK_WORK *wk );


static int (*SubSeqFuncTbl[])( PHCLINK_WORK *wk )={
	SubSeqMsg,
	SubSeqYesNoInit,
	SubSeqYesNoWait,
	SubSeqSelectInit,
	SubSeqSelectMsgWait,
	SubSeqSelectWait,
	SubSeqSelectAction,

	SubSeqWalkClearMsg,
	SubSeqWalkClearMsgWait,
	SubSeqWalkClearYesnoInit,
	SubSeqWalkClearYesnoWait,

	SubSeqStartInitializeMsg,
	SubSeqStartInitializeMsgWait,

	SubSeqInitializeMsg,
	SubSeqInitializeMsgWait,
	SubSeq_Irc_Boot,
	SubSeq_Irc_Command,
	SubSeq_Irc_Wait,
	SubSeq_IrcErrorMsg,
	SUBSEQ_IrcErrorMsgWait,
	SubSeq_IrcSuccessMsg,
	SUBSEQ_IrcSuccessMsgWait,

	SubSeq_ObligateRecvMsg,
	SubSeq_ObligateRecvMsgWait,
	SubSeq_ObligateRecvDemoInit,
	SubSeq_OBligateRecvDemo,
	SubSeq_OBligateRecvDemoEnd,
	SubSeq_ObligateRecvDemoEndMsg,
	SubSeq_ObligateRecvDemoEndMsgWait,

	SubSeq_init_save_msg,
	SubSeq_init_save_msg_wait,
	SubSeq_init_save_start,
	SubSeq_init_save_end,


	SubSeqCursorAnimeWait,
	SubSeqEnd,
};


//==============================================================================
/**
 * @brief   ＰＨＣ完全初期化アプリ開始
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  アプリシーケンスNO
 */
//==============================================================================
int PhcLink_SeqInitialize_Init( PHCLINK_WORK *wk )
{
	OS_Printf("PhcLink_SeqInitialize_Init\n");
	// 上画面ウインドウ面消去
	GF_BGL_ScrFill(	wk->bgl, GF_BGL_FRAME0_S, 0x0000, 0, 0, 32, 24, GF_BGL_SCRWRT_PALNL );
	GF_BGL_ScrFill(	wk->bgl, GF_BGL_FRAME1_S, 0x0000, 0, 0, 32, 24, GF_BGL_SCRWRT_PALNL );
	GF_BGL_ScrFill(	wk->bgl, GF_BGL_FRAME1_M, 0x0000, 0, 0, 32, 24, GF_BGL_SCRWRT_PALNL );
	GF_BGL_ScrFill(	wk->bgl, GF_BGL_FRAME2_M, 0x0000, 0, 0, 32, 24, GF_BGL_SCRWRT_PALNL );
	PhcLinkObj_PhcDisappear(wk);
	PhcLinkObj_Disappear( wk, PLOBJ_ID_POKEMON_U );
	GF_BGL_LoadScreenReq( wk->bgl, GF_BGL_FRAME0_S);
	GF_BGL_LoadScreenReq( wk->bgl, GF_BGL_FRAME1_S);
	GF_BGL_LoadScreenReq( wk->bgl, GF_BGL_FRAME1_M);
	GF_BGL_LoadScreenReq( wk->bgl, GF_BGL_FRAME2_M);

	return SEQ_MAINSEQ_MAIN;
}

//==============================================================================
/**
 * @brief   PHC完全初期化アプリメイン
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  アプリシーケンスNO
 */
//==============================================================================
int PhcLink_SeqInitialize_Main( PHCLINK_WORK *wk )
{

//	OS_Printf("PhcLink_SeqInitialize_Main\n");
	wk->subseq = SubSeqFuncTbl[wk->subseq]( wk );
	if(wk->subseq == SUBSEQ_MAX){
		wk->subseq  = 0;
		return SEQ_MAINSEQ_END;
	}

	PhcLinkObj_LightObjMain( wk );

	return SEQ_MAINSEQ_MAIN;
}
//==============================================================================
/**
 * @brief   PHC完全初期化アプリ終了
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  アプリシーケンスNO
 */
//==============================================================================
int PhcLink_SeqInitialize_End( PHCLINK_WORK *wk )
{
	PhcLinkObj_Disappear( wk, PLOBJ_ID_POKEMON_D );
	PhcLinkObj_PhcDisappear(wk);

	OS_Printf("PhcLink_SeqInitialize_End\n");
	return SEQ_MAINSEQ_INIT;
}



//------------------------------------------------------------------
/**
 * @brief   「ちゅうい！あずけていたポケモンは…」
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//------------------------------------------------------------------
static int SubSeqMsg( PHCLINK_WORK *wk )
{
	wk->MsgIndex = PhcLinkBmp_TalkWinPrint( wk, PL_BMP_B_TALK, phc_ds_msg_04 );

	return  SUBSEQ_YESNO_INIT;

}
//------------------------------------------------------------------
/**
 * @brief   会話終了待ち＆はい・いいえ開始
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//------------------------------------------------------------------
static int SubSeqYesNoInit( PHCLINK_WORK *wk )
{
	if(GF_MSG_PrintEndCheck( wk->MsgIndex )==0){

		// はい・いいえ初期化
		PhcLinkBmp_YesnoInit( wk, PHC_YESNO_NO );

		return SUBSEQ_YESNO_WAIT;
	}
	return  SUBSEQ_YESNO_INIT;
}


//------------------------------------------------------------------
/**
 * @brief   はい・いいえ待ち
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//------------------------------------------------------------------
static int SubSeqYesNoWait( PHCLINK_WORK *wk )
{

	switch(PhcLinkBmp_YesnoWait(wk)){
	case PHC_YESNO_YES:
		PhcLinkObj_CursorDisappear( wk );
		GF_BGL_ScrFill(	wk->bgl, GF_BGL_FRAME1_M, 0x0000, 0, 16, 32, 8, GF_BGL_SCRWRT_PALNL );
		GF_BGL_BmpWinOff( &wk->MainBmpWin[ PL_BMP_B_SEL_2_0 ] );
		GF_BGL_BmpWinOff( &wk->MainBmpWin[ PL_BMP_B_SEL_2_1 ] );
		return SUBSEQ_SELECT_INIT;
		break;
	case PHC_YESNO_NO:
//		GF_BGL_ScrFill(	wk->bgl, GF_BGL_FRAME1_M, 0x0000, 0, 16, 32, 8, GF_BGL_SCRWRT_PALNL );
//		GF_BGL_BmpWinOff( &wk->MainBmpWin[ PL_BMP_B_SEL_2_0 ] );
//		GF_BGL_BmpWinOff( &wk->MainBmpWin[ PL_BMP_B_SEL_2_1 ] );
//		PhcLinkObj_CursorDisappear( wk );
		wk->mainseq_next = MAINSEQ_END_PHC;
		return SUBSEQ_END;
		break;
	default:
		break;
	}

	return SUBSEQ_YESNO_WAIT;
}


//------------------------------------------------------------------
/**
 * @brief   「PHCのきろくをすべてけします」表示
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//------------------------------------------------------------------
static int SubSeqSelectInit( PHCLINK_WORK *wk )
{

	wk->MsgIndex = PhcLinkBmp_TalkWinPrint( wk, PL_BMP_B_MINITALK, phc_ds_msg_05 );

	GF_BGL_ScrFill(	wk->bgl, GF_BGL_FRAME2_M, 0x0000, 0, 0, 32, 24, GF_BGL_SCRWRT_PALNL );
	GF_BGL_LoadScreenReq( wk->bgl, GF_BGL_FRAME2_M );

	return SUBSEQ_SELECT_MSG_WAIT;
}


//------------------------------------------------------------------
/**
 * @brief   メッセージ待ち・選択肢開始
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//------------------------------------------------------------------
static int SubSeqSelectMsgWait( PHCLINK_WORK *wk )
{
	if(GF_MSG_PrintEndCheck( wk->MsgIndex )==0){
		PhcLinkBmp_StrPrintColor_M( wk, PL_BMP_B_SEL_1, wk->SelectStr[ PL_STRING_KESU ],
									FONT_TOUCH, PHCCOL_B_BLACK );
		PhcLink_BgGraphicSet( wk, GF_BGL_FRAME2_M, NARC_phc_ds_phcds_btn_d_lz_nscr );
		PhcLinkBmp_CancelPrint( wk, PL_BMP_B_CANCEL );			// 「やめる」表示
		PhcLinkObj_CursorPosAppearSet( wk, 2 );
		return SUBSEQ_SELECT_WAIT;

	}
	return SUBSEQ_SELECT_MSG_WAIT;
}



//------------------------------------------------------------------
/**
 * @brief   初期化する・しない？選択待ち
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//------------------------------------------------------------------
static int SubSeqSelectWait( PHCLINK_WORK *wk )
{
	u32 touch =	PhcLink_GetTouchTrg( PHC_TOUCH_1_1_MODE );

	if(touch!=RECT_HIT_NONE){
		PhcLink_CursorAnimeStart( wk, touch, 2+touch, SUBSEQ_SELECT_ACTION );
		Snd_SePlay(SEQ_SE_DP_DECIDE);
		return SUBSEQ_CURSOR_ANIME_WAIT;
	}
	if(sys.trg&PAD_KEY_UP){
		if(wk->cursor_pos!=0){
			Snd_SePlay(SEQ_SE_DP_SELECT);
		}
		PhcLinkObj_CursorPosAppearSet( wk, 2);
		wk->cursor_pos = 0;
	}else if(sys.trg&PAD_KEY_DOWN){
		if(wk->cursor_pos!=1){
			Snd_SePlay(SEQ_SE_DP_SELECT);
		}
		PhcLinkObj_CursorPosAppearSet( wk, 3);
		wk->cursor_pos = 1;
	}else if(sys.trg&PAD_BUTTON_DECIDE){
		Snd_SePlay(SEQ_SE_DP_DECIDE);
		PhcLink_CursorAnimeStart( wk, wk->cursor_pos, 2+wk->cursor_pos, SUBSEQ_SELECT_ACTION );
		return SUBSEQ_CURSOR_ANIME_WAIT;
	}else if(sys.trg&PAD_BUTTON_CANCEL){
		Snd_SePlay(SEQ_SE_DP_SELECT);
		PhcLink_CursorAnimeStart( wk, 1, 3, SUBSEQ_SELECT_ACTION );
		return SUBSEQ_CURSOR_ANIME_WAIT;
	}

	return SUBSEQ_SELECT_WAIT;
}


//----------------------------------------------------------------------------------
/**
 * @brief    決定項目を実行する
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeqSelectAction( PHCLINK_WORK *wk )
{
	// カーソルオフ
	PhcLinkObj_CursorDisappear( wk );
	GF_BGL_BmpWinOff( &wk->MainBmpWin[ PL_BMP_B_SEL_1 ] );
	GF_BGL_ScrFill(	wk->bgl, GF_BGL_FRAME1_M, 0x0000, 0, 16, 32, 8, GF_BGL_SCRWRT_PALNL );

	if(wk->cursor_pos==0){
		// 初期化開始メッセージ
		return SUBSEQ_START_INITIALIZE_MSG;
	}

	// touch==1 最初に戻る
	wk->mainseq_next = MAINSEQ_END_PHC;
	return SUBSEQ_END;

}

//----------------------------------------------------------------------------------
/**
 * @brief    「ポケウォーカーのほすうはしょきかしますか？」表示
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int	SubSeqWalkClearMsg( PHCLINK_WORK *wk )
{
	GF_BGL_ScrFill(	wk->bgl, GF_BGL_FRAME2_M, 0x0000, 0, 0, 32, 24, GF_BGL_SCRWRT_PALNL );
	GF_BGL_ScrFill(	wk->bgl, GF_BGL_FRAME1_M, 0x0000, 0, 0, 32, 24, GF_BGL_SCRWRT_PALNL );
	GF_BGL_LoadScreenReq( wk->bgl, GF_BGL_FRAME1_M );
	GF_BGL_LoadScreenReq( wk->bgl, GF_BGL_FRAME2_M );

	wk->MsgIndex = PhcLinkBmp_TalkWinPrint( wk, PL_BMP_B_TALK, phc_ds_msg_10 );

	return SUBSEQ_WALK_CLEAR_MSG_WAIT;
}

//----------------------------------------------------------------------------------
/**
 * @brief    「ポケウォーカーのほすうはしょきかしますか？」表示待ち
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int	SubSeqWalkClearMsgWait( PHCLINK_WORK *wk )
{
	if(GF_MSG_PrintEndCheck( wk->MsgIndex )==0){
		return SUBSEQ_WALK_CLEAR_YESNO_INIT;
	}
	return SUBSEQ_WALK_CLEAR_MSG_WAIT;

}

//----------------------------------------------------------------------------------
/**
 * @brief    「はい・いいえ」開始
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int	SubSeqWalkClearYesnoInit( PHCLINK_WORK *wk )
{
	// はい・いいえ初期化
	PhcLinkBmp_StepClearYesnoInit( wk, PHC_YESNO_NO );


	return SUBSEQ_WALK_CLEAR_YESNO_WAIT;
}


//----------------------------------------------------------------------------------
/**
 * @brief    「はい・いいえ」待ち
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int	SubSeqWalkClearYesnoWait( PHCLINK_WORK *wk )
{

	switch(PhcLinkBmp_YesnoWait(wk)){
	case PHC_YESNO_YES:
		PhcLinkObj_CursorDisappear( wk );
		GF_BGL_ScrFill(	wk->bgl, GF_BGL_FRAME1_M, 0x0000, 0, 16, 32, 8, GF_BGL_SCRWRT_PALNL );
		GF_BGL_BmpWinOff( &wk->MainBmpWin[ PL_BMP_B_SEL_2_0 ] );
		GF_BGL_BmpWinOff( &wk->MainBmpWin[ PL_BMP_B_SEL_2_1 ] );
		wk->clear_step = PHC_TOTALSTEP_NO_CLEAR;		// 歩数をクリアしない
		return SUBSEQ_START_INITIALIZE_MSG;
		break;
	case PHC_YESNO_NO:
		GF_BGL_ScrFill(	wk->bgl, GF_BGL_FRAME1_M, 0x0000, 0, 16, 32, 8, GF_BGL_SCRWRT_PALNL );
		GF_BGL_BmpWinOff( &wk->MainBmpWin[ PL_BMP_B_SEL_2_0 ] );
		GF_BGL_BmpWinOff( &wk->MainBmpWin[ PL_BMP_B_SEL_2_1 ] );
		PhcLinkObj_CursorDisappear( wk );
		wk->clear_step = PHC_TOTALSTEP_CLEAR;			// 歩数をクリアする
		return SUBSEQ_START_INITIALIZE_MSG;
		break;
	default:
		break;
	}

	return SUBSEQ_WALK_CLEAR_YESNO_WAIT;

}


//----------------------------------------------------------------------------------
/**
 * @brief    「DSとのつうしんをかいしします」表示
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeqStartInitializeMsg( PHCLINK_WORK *wk )
{
	GF_BGL_ScrClear( wk->bgl, GF_BGL_FRAME0_S );
	GF_BGL_ScrClear( wk->bgl, GF_BGL_FRAME1_S );
	GF_BGL_ScrClear( wk->bgl, GF_BGL_FRAME1_M );
	GF_BGL_ScrClear( wk->bgl, GF_BGL_FRAME2_M );

	// メッセージ表示スタート（この関数の最後が特殊）
	PhcLink_MsgStart( wk, PL_BMP_B_TALK, phc_ds_msg_17, SUBSEQ_INITIALIZE_MSG );

	return SUBSEQ_START_INITIALIZE_MSG_WAIT;

}

//----------------------------------------------------------------------------------
/**
 * @brief    メッセージ表示待ち
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeqStartInitializeMsgWait( PHCLINK_WORK *wk )
{
	if(GF_MSG_PrintEndCheck( wk->MsgIndex )==0){
		wk->subseq = wk->subseq_next;
		return wk->subseq;
	}
	return SUBSEQ_START_INITIALIZE_MSG_WAIT;

}

//----------------------------------------------------------------------------------
/**
 * @brief    「ポケウォーカーをこちらにむけ…」表示
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeqInitializeMsg( PHCLINK_WORK *wk )
{

	GF_BGL_ScrClear( wk->bgl, GF_BGL_FRAME1_M );
	GF_BGL_ScrFill(	wk->bgl, GF_BGL_FRAME2_M, 0x0000, 0, 0, 32, 24, GF_BGL_SCRWRT_PALNL );
	PhcLink_BgGraphicSet( wk, GF_BGL_FRAME2_M, NARC_phc_ds_phcds_back_d_lz_nscr );

	PhcLinkBmp_CancelPrint( wk, PL_BMP_B_CANCEL );			// 「やめる」表示

	// 上画面PHC接続説明表示開始
	PhcLink_AddBg1ScrolTask(wk);

	wk->MsgIndex = PhcLinkBmp_TalkWinPrint( wk, PL_BMP_B_MINITALK, phc_ds_msg_06 );

	return SUBSEQ_INITIALIZE_MSG_WAIT;
}


//----------------------------------------------------------------------------------
/**
 * @brief    メッセージ表示待ち
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeqInitializeMsgWait( PHCLINK_WORK *wk )
{

	if(GF_MSG_PrintEndCheck( wk->MsgIndex )==0 && wk->Bg1SubHOffset==256){
		PhcLinkObj_PhcAppear( wk, 2 );
		return SUBSEQ_IRC_BOOT;
	}
	return SUBSEQ_INITIALIZE_MSG_WAIT;
}


//----------------------------------------------------------------------------------
/**
 * @brief    ＰＨＣ初期化用赤外線ポート起動
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeq_Irc_Boot( PHCLINK_WORK *wk )
{
	// 初期化コマンド発行(歩数をクリアするかどうかは一つ前の選択で決めてる）

	PHC_Cleanup( PHC_TOTALSTEP_CLEAR );

	// PHCから受信データ領域をクリア
	MI_CpuClearFast( &wk->phcsys.Distribute, sizeof(PHCDistributeData));
	MI_CpuClearFast( &wk->phcsys.Data, 		 sizeof(PHCData));


	return SUBSEQ_IRC_COMMAND;
}


static int SubSeq_Irc_Command( PHCLINK_WORK *wk )
{
	// PHC赤外線ライブラリ起動
	PhcLink_IrcSystemInit( wk );

	return SUBSEQ_IRC_WAIT;
}


//----------------------------------------------------------------------------------
/**
 * @brief    ＰＨＣ初期化用赤外線ポート起動
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeq_Irc_Wait( PHCLINK_WORK *wk )
{
	int result = SUBSEQ_IRC_WAIT;
	// エラー検出
 	PHCError error = PHC_GetError();
	/****************************************************************************
	****************************************************************************/

	switch(error){
	case PHC_ERROR_TIMEOUT:
		OS_Printf("通信タイムアウト\n");
		result = SUBSEQ_IRC_ERROR_MSG;
		break;
	case PHC_ERROR_INITIALIZED:
		OS_Printf("すでに登録済みのPHCです\n");
		result = SUBSEQ_IRC_ERROR_MSG;
		break;
	case PHC_ERROR_NOT_INITIALIZED:
		OS_Printf("未登録のPHCです\n");
		result = SUBSEQ_IRC_ERROR_MSG;
		break;
	case PHC_ERROR_NOT_EMPTY:
		OS_Printf("既にポケモンを預かっている\n");
		result = SUBSEQ_IRC_ERROR_MSG;
		break;
	case PHC_ERROR_MUCH_COUNTRY:
		OS_Printf("国バージョンが異なります\n");
		result = SUBSEQ_IRC_ERROR_MSG;
		break;
	case PHC_ERROR_MUCH_ROM:
		OS_Printf("ROMバージョンが異なります\n");
		result = SUBSEQ_IRC_ERROR_MSG;
		break;
	case PHC_ERROR_MUCH_TRAINER:
		OS_Printf("トレーナーIDが異なります\n");
		result = SUBSEQ_IRC_ERROR_MSG;
		break;
	case PHC_ERROR_MUCH_UNIT:
		OS_Printf("この相手とは通信できない\n");
		result = SUBSEQ_IRC_ERROR_MSG;
		break;
	case PHC_ERROR_DONE:
		OS_Printf("初期化成功した\n");

		// PHCのアニメを終了
		CATS_ObjectEnableCap( wk->cap[ PLOBJ_ID_PHC_ANIME ], ACT_OFF );

		// ポケモンを預けてないかチェック
		_deposit_pokemon_check( wk );

		// PHCセーブデータクリア
		PhcSvData_Clear( wk->phc_svdata );
//		// 初期登録なので、歩数はPHCから引継ぎ、wattは０クリアする
//		PhcSvData_SetTotalStepWat( wk->phc_svdata, wk->phcsys.myStatusPHC.totalStep, 0 );
		wk->phcsys.old_total_step = 0;		// プログラム内ワークもクリア
		wk->phcsys.old_total_wat  = 0;

		// PHC通信終了
		PHC_Dispose();

		return SUBSEQ_IRC_SUCCESS_MSG;
		break;
	}

	// エラー終了・通常成功ともにPHCライブラリを停止させる
	if(result!=SUBSEQ_IRC_WAIT){
		PHC_Dispose();
	}

	// 赤外線の接続状態でなければキャンセル可能
	if(!PHC_IsConnect()){
		PhcLink_SetIrcExplainVisible( wk, 1 );
		if((PhcLink_GetTouchTrg( PHC_TOUCH_CANCEL_MODE )==0)
		 || (sys.trg & PAD_BUTTON_DECIDE) || (sys.trg & PAD_BUTTON_CANCEL) ){
			PHC_Dispose();
			PhcLink_CursorAnimeStart( wk, 1, 3, SUBSEQ_IRC_ERROR_MSG );
			Snd_SePlay(SEQ_SE_DP_SELECT);
			return SUBSEQ_CURSOR_ANIME_WAIT;
		}
	}else{
		PhcLink_SetIrcExplainVisible( wk, 0 );
	}


//	OS_Printf("error=%d\n", error);
//	return SUBSEQ_IRC_ERROR_MSG;
	return result;
}



//----------------------------------------------------------------------------------
/**
 * @brief    セーブデータにポケモンを残しているかチェック（いるなら戻す）
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static void _deposit_pokemon_check( PHCLINK_WORK *wk )
{
	int boxNo=-1, boxPos=0, result;
	u16 flag, phcBox;
	BOX_DATA *boxdata       = SaveData_GetBoxData( wk->savedata );
	POKEMON_PASO_PARAM *ppp = (POKEMON_PASO_PARAM*)wk->pokepaso;

	// PHC預けた情報の取得
	PhcSvData_GetDepositInfo( wk->phc_svdata, &flag , &phcBox );

	result = PhcSvData_GetPokemon( wk->phc_svdata, (u8*)ppp );

	OS_Printf("初期化時のポケモン保存は,%d\n", result);

	// ポケモンがセーブデータに存在しているか？
	if(result){
		boxNo = phcBox;

		// ボックスの空いてる場所を検索
		BOXDAT_GetEmptyTrayNumberAndPos( boxdata, &boxNo, &boxPos );

		// ポケモン格納
		BOXDAT_PutPokemonPos( boxdata, boxNo, boxPos, ppp );

		wk->box = boxNo;
	}else{
		wk->box = -1;
	}


}
//----------------------------------------------------------------------------------
/**
 * @brief    エラー時のメッセージ表示
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeq_IrcErrorMsg( PHCLINK_WORK *wk )
{
	// ポケウォーカーのつうしんをやめました
	wk->MsgIndex = PhcLinkBmp_TalkWinPrint( wk, PL_BMP_B_MINITALK, phc_error_01 );
	PhcLinkObj_CursorDisappear( wk );

	GF_BGL_ScrFill(	wk->bgl, GF_BGL_FRAME2_M, 0x0000, 0, 0, 32, 24, GF_BGL_SCRWRT_PALNL );

	return SUBSEQ_IRC_ERROR_MSG_WAIT;
}

//----------------------------------------------------------------------------------
/**
 * @brief    エラーメッセージ表示待ち
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SUBSEQ_IrcErrorMsgWait( PHCLINK_WORK *wk )
{
	if(GF_MSG_PrintEndCheck( wk->MsgIndex )==0){
		wk->mainseq_next = MAINSEQ_END_PHC;
		return SUBSEQ_END;
	}
	return SUBSEQ_IRC_ERROR_MSG_WAIT;

}


//----------------------------------------------------------------------------------
/**
 * @brief    「しょきかに　せいこうしました」
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeq_IrcSuccessMsg( PHCLINK_WORK *wk )
{

	GF_BGL_ScrFill(	wk->bgl, GF_BGL_FRAME2_M, 0x0000, 0, 0, 32, 24, GF_BGL_SCRWRT_PALNL );
	GF_BGL_ScrFill(	wk->bgl, GF_BGL_FRAME1_S, 0x0000, 0, 0, 64, 24, GF_BGL_SCRWRT_PALNL );
	GF_BGL_LoadScreenReq( wk->bgl, GF_BGL_FRAME2_M );
	GF_BGL_LoadScreenReq( wk->bgl, GF_BGL_FRAME1_S );

	wk->MsgIndex = PhcLinkBmp_TalkWinPrint( wk, PL_BMP_B_TALK, phc_com_msg_08 );

	GF_BGL_ScrFill(	wk->bgl, GF_BGL_FRAME2_M, 0x0000, 0, 0, 32, 24, GF_BGL_SCRWRT_PALNL );

	return SUBSEQ_IRC_SUCCESS_MSG_WAIT;
}


//----------------------------------------------------------------------------------
/**
 * @brief    ＰＨＣ初期化成功時メッセージ表示待ち
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SUBSEQ_IrcSuccessMsgWait( PHCLINK_WORK *wk )
{
	if(GF_MSG_PrintEndCheck( wk->MsgIndex )==0){
		wk->mainseq_next = MAINSEQ_START;
		return SUBSEQ_OBLIGATE_RECV_MSG;

	}
	return SUBSEQ_IRC_SUCCESS_MSG_WAIT;

}


//----------------------------------------------------------------------------------
/**
 * @brief    ポケモンを預けていたらメッセージで復帰を話す
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeq_ObligateRecvMsg( PHCLINK_WORK *wk )
{
	// ポケモン預けてた
	if(wk->box>=0){
		// 「ポケモンもどってこい！」
		WORDSET_RegisterPokeNickName( wk->wset, 1, (POKEMON_PASO_PARAM*)wk->pokepaso );
		wk->MsgIndex = PhcLinkBmp_TalkWinPrint( wk, PL_BMP_B_TALK, phc_com_msg_03 );
		return SUBSEQ_OBLIGATE_RECV_MSG_WAIT;
	}

	// ポケモン預けてなかった
	return SUBSEQ_INIT_SAVE_MSG;
}

//----------------------------------------------------------------------------------
/**
 * @brief     ポケモンを預けていたらメッセージで（選択待ち）
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeq_ObligateRecvMsgWait( PHCLINK_WORK *wk )
{
	if(GF_MSG_PrintEndCheck( wk->MsgIndex )==0){
//		wk->mainseq_next = MAINSEQ_START;
		return SUBSEQ_OBLIGATE_RECV_DEMO_INIT;

	}
	return SUBSEQ_OBLIGATE_RECV_MSG_WAIT;

}


//----------------------------------------------------------------------------------
/**
 * @brief    戻ってくるデモ初期化
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeq_ObligateRecvDemoInit( PHCLINK_WORK *wk )
{
	wk->time = 0;

	PhcLinkObj_Disappear( wk, PLOBJ_ID_POKEMON_D );

	// 正面絵画像転送
	PhcLink_TransPokeGra( (POKEMON_PASO_PARAM *)wk->pokepaso, wk->PokeGra, &wk->PokeGraSsa, 0 );

	return SUBSEQ_OBLIGATE_RECV_DEMO;
}


#define OBLIGATE_DEMO_TIME	( 180 )		///< 強制変換デモタイム

//----------------------------------------------------------------------------------
/**
 * @brief    戻ってくるデモ
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeq_OBligateRecvDemo( PHCLINK_WORK *wk )
{
	wk->time++;
	if(wk->time> OBLIGATE_DEMO_TIME){
		// ポケモン表示
		PhcLinkObj_Appear( wk, PLOBJ_ID_POKEMON_D );
		return SUBSEQ_OBLIGATE_RECV_DEMO_END;

	}
	if(wk->time > (gf_mtRand()%OBLIGATE_DEMO_TIME)){
		PhcLinkObj_DownLightObjBirth( wk );
	}

	return SUBSEQ_OBLIGATE_RECV_DEMO;
}


//----------------------------------------------------------------------------------
/**
 * @brief    戻ってくるデモ終了
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeq_OBligateRecvDemoEnd( PHCLINK_WORK *wk )
{
	return SUBSEQ_OBLIGATE_RECV_DEMO_END_MSG;
}

//----------------------------------------------------------------------------------
/**
 * @brief    「○○○がもどってきた！」表示
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeq_ObligateRecvDemoEndMsg( PHCLINK_WORK *wk )
{
	wk->MsgIndex = PhcLinkBmp_TalkWinPrint( wk, PL_BMP_B_TALK, phc_com_msg_04 );
	return SUBSEQ_OBLIGATE_RECV_DEMO_END_MSG_WAIT;
}

//----------------------------------------------------------------------------------
/**
 * @brief    「○○○がもどってきた！」表示待ち
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeq_ObligateRecvDemoEndMsgWait( PHCLINK_WORK *wk )
{
	if(GF_MSG_PrintEndCheck( wk->MsgIndex )==0){
		return SUBSEQ_INIT_SAVE_MSG;

	}
	return SUBSEQ_OBLIGATE_RECV_DEMO_END_MSG_WAIT;
}


//----------------------------------------------------------------------------------
/**
 * @brief    セーブします。
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeq_init_save_msg( PHCLINK_WORK *wk )
{
	wk->MsgIndex = PhcLinkBmp_TalkWinPrint( wk, PL_BMP_B_TALK, phc_ds_msg_14 );

	return SUBSEQ_INIT_SAVE_MSG_WAIT;

}


//----------------------------------------------------------------------------------
/**
 * @brief    セーブ処理時計アイコンアニメ開始
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeq_init_save_msg_wait( PHCLINK_WORK *wk )
{
	if(GF_MSG_PrintEndCheck( wk->MsgIndex )==0){
//		wk->mainseq_next = MAINSEQ_START;
		PhcLinkBmp_TimeWaitIconPut( wk );
		return SUBSEQ_INIT_SAVE_START;
	}

	return SUBSEQ_INIT_SAVE_MSG_WAIT;
}


//----------------------------------------------------------------------------------
/**
 * @brief    セーブ
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeq_init_save_start( PHCLINK_WORK *wk )
{
	SaveData_Save( wk->savedata );
	SaveData_ResetLoadResult(wk->savedata);

	return SUBSEQ_INIT_SAVE_END;

}

//----------------------------------------------------------------------------------
/**
 * @brief    セーブ終了
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeq_init_save_end( PHCLINK_WORK *wk )
{
	PhcLinkBmp_TimeWaitIconDel( wk );
	wk->mainseq_next = MAINSEQ_START;
	return SUBSEQ_END;
}



//----------------------------------------------------------------------------------
/**
 * @brief    カーソルアニメ待ち
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//----------------------------------------------------------------------------------
static int SubSeqCursorAnimeWait( PHCLINK_WORK *wk )
{
	if(CATS_ObjectAnimeActiveCheckCap( wk->cap[PLOBJ_ID_CURSOR])==FALSE){
		wk->subseq = wk->subseq_next;
		return wk->subseq;
	}
	return SUBSEQ_CURSOR_ANIME_WAIT;

}

//------------------------------------------------------------------
/**
 * @brief   シーケンス終了
 *
 * @param   wk	PHC画面ワーク
 *
 * @retval  サブシーケンスNO
 */
//------------------------------------------------------------------
static int SubSeqEnd( PHCLINK_WORK *wk )
{
	PhcLinkObj_CursorDisappear( wk );

	return SUBSEQ_MAX;
}


//----------------------------------------------------------------------------------
/**
 * @brief    カーソルアニメスタート
 *
 * @param   wk		PHC画面ワーク
 * @param   pos		選択された行動
 * @param   cursor	カーソルの位置（phclink内で統一してるので、posと違うことも有る）
 * @param   next	カーソル終了後のシーケンス
 */
//----------------------------------------------------------------------------------
void PhcLink_CursorAnimeStart( PHCLINK_WORK *wk, int pos, int cursor, int next )
{
	// 選択肢保存
	wk->cursor_pos = pos;

	// カーソル位置移動
	PhcLinkObj_CursorPosAppearSet( wk, cursor );

	// カーソル点滅スタート
	PhcLinkObj_CursorBlink( wk );

	wk->subseq_next = next;

}

//=============================================================================================
/**
 * @brief    メッセージ表示開始
 *
 * @param   wk		PHC画面ワーク
 * @param   winidx	BMPWINのINDEX（phclink_bmp_def.h参照）
 * @param   msg		表示するメッセージID
 * @param   next	終了後のサブシーケンス
 */
//=============================================================================================
void PhcLink_MsgStart( PHCLINK_WORK *wk, int winidx, int msg, int next )
{
	wk->MsgIndex = PhcLinkBmp_TalkWinPrint( wk, winidx, msg );
	wk->subseq_next = next;
}
